home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / autoload.cmm < prev    next >
Text File  |  1995-10-11  |  14KB  |  486 lines

  1. /*
  2.  * Autoload.cmm
  3.  *
  4.  * This file handles most of the shell interface of CEnvi. The rest of the
  5.  * interface is included as the file "ShellChr.cmm". This version will run
  6.  * on all CEnvi systems known as of the revision date.
  7.  *
  8.  */
  9.  
  10.  
  11.  
  12. if( !defined(_NWNLM_) && !defined(_DOS_) && !defined(_DOS32_) && !defined(_OS2_) &&
  13.     !defined(_WINDOWS_) && !defined(_NTCON_) && !defined(_NTWIN_) )
  14. {
  15.   printf("\nThe system you are running CEnvi on is not supported by this\n"
  16.          "version of AUTOLOAD.CMM. A newer version should have been included\n"
  17.          "in your distribution. You should use that instead.\n");
  18.   exit(1);
  19. }
  20.  
  21. if( !defined(_SHELL_) )
  22. {
  23.   printf("Autoload.cmm is an internal shell implementation file and cannot be\n");
  24.   printf("run as a program.\n");
  25.   exit(EXIT_FAILURE);
  26. }
  27.  
  28. /* ---------------------------------------------------------------------- */
  29.  
  30. // First we have a list of commands that are to be passed on directly to
  31. // the shell. Windows and Netware have no such list of commands.
  32. if( defined(_DOS_) || defined(_DOS32_) )
  33. {
  34.   SystemCommands =
  35.     {
  36.       "DIR",      "COPY",     "MKDIR",    "MD",       "RMDIR",
  37.       "RD",       "ERASE",    "DEL",      "TYPE",     "ECHO"
  38.     };
  39. }
  40. else if( defined(_OS2_) )
  41. {
  42.   SystemCommands =
  43.     {
  44.       "DIR",      "MKDIR",    "MD",       "RMDIR",    "RD",
  45.       "ERASE",    "DEL",      "TYPE",     "START",    "DETACH",
  46.       "COPY",     "ECHO"
  47.     };
  48. }
  49. else if( defined(_NTCON_) )
  50. {
  51.   SystemCommands =
  52.     {
  53.       "DIR",      "COPY",     "MKDIR",    "MD",       "RMDIR",
  54.       "RD",       "ERASE",    "DEL",      "TYPE",     "ECHO",
  55.       "START"
  56.     };
  57. }
  58. if( defined(SystemCommands) ) qsort(SystemCommands,"stricmp");
  59.  
  60.  
  61. // Some Basic Aliases
  62. alias_list = {
  63.   { "md",      "makedir" },
  64.   { "mkdir",   "makedir" },
  65.   { "clear",   "cls" },
  66.   { "erase",   "del" },
  67.   { "rm",      "del" },
  68.   { "cp",      "copy" },
  69.   { "mv",      "move" },
  70.   { "ren",     "move" },
  71.   { "rename",  "move" },
  72.   { "chmod",   "attrib" },
  73.   { "rd",      "rmdir" },
  74.   { "load",    "start" }
  75. };
  76.  
  77. if ( defined(_DOS_) || defined(_DOS32_) ) {
  78.    // spawning can leave too-little memory, so increase COMSPEC size
  79.    if ( !defined(COMSPEC_ENV_SIZE) )
  80.       COMSPEC_ENV_SIZE = 2000;
  81. }
  82.  
  83. ///////////////////////////////////////////////////////////////////////
  84.  
  85.  
  86. #include "CmdLine.lib"
  87.  
  88. // The other systems all have this defined. Netware has no Environment variables
  89. // so we define it here.
  90.  
  91. if( defined(_NWNLM_) ) PROMPT = NULL;
  92.  
  93. if ( !defined(_WINDOWS_) && !defined(_NTWIN_)
  94.   && defined(PROMPT) && strnicmp("[CEnvi]",PROMPT,7) )
  95.   {
  96.     strcpy(oldprompt,PROMPT);
  97.     sprintf(PROMPT,"[CEnvi] %s",PROMPT);
  98.     getenv();
  99.   }
  100.  
  101. /*
  102.  * Change the current directory to the given one. Return 0 if successful.
  103.  */
  104. my_chdir(newdir)
  105. {
  106.   if( defined(_NWNLM_) ) return chdir(newdir);
  107.   if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
  108.     {
  109.       lReg.ah = 0x3B;
  110.       if ( !defined(_DOS32_) )
  111.          lReg.ds = segment(newdir), lReg.dx = offset(newdir);
  112.       else
  113.          lReg.dx = pointer(newdir);
  114.       interrupt(0x21,lReg,out);
  115.       return out.ax==3;
  116.     }
  117.   if( defined(_NTCON_) || defined(_NTWIN_) )
  118.     {
  119.       return !DynamicLink("KERNEL32","SetCurrentDirectoryA",STDCALL,newdir);
  120.     }
  121.   if( defined(_OS2_) )
  122.     {
  123. #define ORD_DOS32SETCURRENTDIR   255
  124.       return DynamicLink("doscalls",ORD_DOS32SETCURRENTDIR,BIT32,CDECL,newdir);
  125.     }
  126. }
  127.  
  128. //**************************************************************************
  129. //****** BEGIN SECTION TO HANDLE INTERNAL COMMANDS FOR THE CLI SHELL *******
  130. //**************************************************************************
  131.  
  132.  
  133. // Next is a list of all commands handled internally to the shell. This list
  134. // must remain sorted. Also, the InternalHelp array must match. It is fortunate
  135. // that currently all the 'extra' commands only defined on certain systems
  136. // fall later than any other command in the alphabet.
  137.  
  138. InternalCommands = {
  139.   "ALIAS", "CD", "CHDIR", "CLS", "ECHO", "HELP", "PROMPT", "PWD"
  140. };
  141. InternalHelp = {
  142.   "The ALIAS command lets you view or define aliases. ALIAS by itself will list\n"
  143.   "all aliases currently defined. Otherwise define an alias by using the format:\n"
  144.   "\n  ALIAS name=text string",
  145.   "CD used by itself will print the current directory. If you give it a\ndirectory name, it will change the current directory to it.",
  146.   "CHDIR changes the current directory to the given directory.",
  147.   "CLS clears the screen.", 
  148.   "ECHO prints some text to the screen.",
  149.   "HELP by itself prints all known commands, otherwise it prints help on a command.",
  150.   "PROMPT will change the prompt. By itself, it resets the prompt to the default.",
  151.   "PWD will print the current working directory.",
  152. };
  153.  
  154. if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) || defined(_NTCON_) ||
  155.     defined(_NTWIN_) || defined(_OS2_) )
  156. {
  157.   InternalCommands[GetArraySpan(InternalCommands)+1] = "SET";
  158.   InternalHelp[GetArraySpan(InternalHelp)+1] = 
  159.     "SET allows you to view or change environment variables. SET by itself will\n"
  160.     "list all such variables currently defined. Otherwise define one by using the\n"
  161.     "format:\n\n"
  162.     "  SET variable=value";
  163. }
  164.  
  165. if( defined(_WINDOWS_) || defined(_NTWIN_) || defined(_NWNLM_) )
  166. {
  167.   InternalCommands[GetArraySpan(InternalCommands)+1] = "START";
  168.   InternalCommands[GetArraySpan(InternalCommands)+1] = "TYPE";
  169.   InternalHelp[GetArraySpan(InternalHelp)+1] = 
  170.     "START starts an Executable or NLM running asynchronously.";
  171.   InternalHelp[GetArraySpan(InternalHelp)+1] = 
  172.     "TYPE will echo the contents of a file to the screen.";
  173. }
  174.  
  175. // set up filter to handle each command input
  176. ShellFilterCommand("AutoloadFilterCommand");
  177.  
  178. AutoloadFilterCommand(pCommand)
  179. {
  180.    if ( !stricmp(pCommand,"EXIT") )
  181.      {
  182.        if( defined(oldprompt) ) PROMPT = oldprompt;
  183.      } else {
  184.        strcpy(tmp,pCommand);
  185.        s = strchr(tmp,' ');
  186.        if( s ) s[0] = '\0';
  187.  
  188.        if ( !stricmp(SplitFileName(tmp).name,"autoload") )
  189.          {
  190.            printf("Autoload.cmm is an internal shell implementation file and cannot be\n");
  191.            printf("run as a program.\n");
  192.            pCommand[0] = '\0';
  193.          } else {
  194.            strcpy(command,pCommand); pCommand[0] = '\0';
  195.            ProcessCommandLine(command,"AutoloadFilterExecutor");
  196.          }
  197.      }
  198. }
  199.  
  200. AutoloadFilterExecutor(pCommand)
  201. {
  202.   lResult = EXIT_SUCCESS;
  203.   undefine(alias);
  204.   alias = "";
  205.   for( i=0;pCommand[i];i++ )
  206.     {
  207.       if( isspace(pCommand[i]) ) break;
  208.       alias[i] = pCommand[i];
  209.     }
  210.   the_rest = "";
  211.   strcpy(the_rest,pCommand+i);
  212.   alias[i] = 0;
  213.   for( j=0;j<=GetArraySpan(alias_list);j++ )
  214.     {
  215.       if( !stricmp(alias,alias_list[j][0]) )
  216.         {
  217.           pCommand = "";
  218.           strcpy(pCommand,alias_list[j][1]);
  219.           strcat(pCommand,the_rest);
  220.           break;
  221.         }
  222.     }
  223.  
  224.    // if line starts with a quote, it is cmm code to execute immediately.
  225.    if ( strchr("\"\'`",pCommand[0]) ) {
  226.       if( pCommand[strlen(pCommand)-1]==pCommand[0] )
  227.         pCommand[strlen(pCommand)-1] = 0;
  228.       lResult = interpret(pCommand+1,INTERP_TEXT|INTERP_NOINHERIT_GLOBAL|
  229.                                      INTERP_NOINHERIT_LOCAL);
  230.       pCommand[0] = 0;
  231.    }
  232.    // if command found in SystemCommands list then execute it immediately
  233.    // with CMD.EXE then, else check internal cmds and some others
  234.    else if ( 1 == sscanf(pCommand,"%s",lCommand) ) {
  235.       if( defined(SystemCommands) && bsearch(lCommand,SystemCommands,"stricmp") )
  236.         {
  237.           strcpy(command,pCommand); pCommand[0] = '\0'
  238.           if( defined(_OS2_) || defined(_NTCON_) || defined(_NTWIN_) )
  239.             {
  240.               lResult = spawn(P_WAIT,"CMD.EXE","/C",command);
  241.             }
  242.           else if( defined(_DOS_) || defined(_DOS32_) )
  243.             {
  244.               lResult = system(P_SWAP,command);
  245.             }
  246.         } else if ( bsearch(lCommand,InternalCommands,"stricmp") ) {
  247.           sprintf(lInternalFunction,"InternalCommand_